package main

func containsNearbyDuplicate(nums []int, k int) bool {
	set := make(map[int]struct{})
	for i, v := range nums {
		if i > k {
			delete(set, nums[i - k - 1])
		}
		if _, ok := set[v]; ok {
			return true
		}
		set[v] = struct{}{}
	}
	return false
}

func main() {
	println(containsNearbyDuplicate([]int{1,2,3,1}, 2))
}